using Microsoft.IdentityModel.Protocols.WSTrust;
using System.Security;
using System.Security.Permissions;
namespace Net35.Xrm.Sdk.Client
{
	[SecuritySafeCritical]
	[SecurityPermission(SecurityAction.Demand, Unrestricted = true)]
	internal sealed class LiveIdTrustChannelFactory : WSTrustChannelFactory
	{
		internal IdentityProviderTrustConfiguration PolicyConfiguration
		{
			get;
			set;
		}
		public SecurityTokenResponse DeviceTokenResponse
		{
			get;
			set;
		}
		public LiveIdTrustChannelFactory(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress, IdentityProviderTrustConfiguration policyConfiguration) : base(binding, remoteAddress)
		{
			ClientExceptionHelper.ThrowIfNull(policyConfiguration, "policyConfiguration");
			this.PolicyConfiguration = policyConfiguration;
		}
		public override IWSTrustChannelContract CreateChannel(System.ServiceModel.EndpointAddress address, System.Uri via)
		{
			FederatedClientCredentials federatedClientCredentials = base.Credentials as FederatedClientCredentials;
			if (federatedClientCredentials != null)
			{
				if (this.DeviceTokenResponse != null)
				{
					federatedClientCredentials.SecurityTokenHandlerCollectionManager[""].AddOrReplace(new LiveIdUserNameSecurityTokenHandler(this.DeviceTokenResponse));
					base.WSTrustRequestSerializer = new LiveIdRequestSecurityTokenSerializer(this.PolicyConfiguration.Policy);
				}
				else
				{
					federatedClientCredentials.SecurityTokenHandlerCollectionManager[""].AddOrReplace(new LiveIdDeviceSecurityTokenHandler());
				}
			}
			return base.CreateChannel(address, via);
		}
	}
}
